<?php
/**
 * @name Bootstrap
 * @package public
 * @author Truonghm
 * @version $1.0
 * @license http://officex.vn/license
 * @copyright ecommerce.
 * 30/10/2010
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{	
	/*
	 * created by truonghm
	 * created date 30/10/2010
	 * Connect database 
	 */
	
	protected function _initDb()
	{		
		try {			
			$config = Ofx_Config::getConfig();			
			$adapter =  $config->resources->db->adapter;
			$params = $config->resources->db->params;
			
			$db = Zend_Db::factory($adapter,$params);				
			$db->setFetchMode(Zend_Db::FETCH_ASSOC);
	        $db->query("SET CHARACTER SET 'utf8'"); 
			       
	        Zend_Registry::set('db',$db);
	        Zend_Db_Table::setDefaultAdapter($db);
	        
		}catch (Exception $e) {
		    echo 'Connect data erro ',  $e->getMessage(), "\n";
		}
	}	
	
	/**
	 * created by truonghm
	 * created date 30/10/2010
	 * auto loader class define set_include_path
	 */	
	protected function _initAutoloader()
	{
		$loader = Zend_Loader_Autoloader::getInstance();
		$loader->setFallbackAutoloader(true);
		$loader->suppressNotFoundWarnings(false);
	}
	
	/*
	 * created by truonghm
	 * created date 30/10/2010
	 * init session system and databse config 
	 */
	protected function _initSession()
	{
		
		$config = array(
		    'name'              => 'session', //table name as per Zend_Db_Table
		    'primary'           => array(
		        'session_id',   //the sessionID given by PHP
		        'save_path',    //session.save_path
		        'name',         //session name
		    ),
		    'primaryAssignment' => array(		        
		        'sessionId', //first column of the primary key is of the sessionID
		        'sessionSavePath', //second column of the primary key is the save path
		        'sessionName', //third column of the primary key is the session name
		    ),
		    'modifiedColumn'    => 'modified',     //time the session should expire
		    'dataColumn'        => 'session_data', //serialized data
		    'lifetimeColumn'    => 'lifetime',     //end of life for a specific record
		);
		
		Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));		
		Zend_Session::start();
		
    	$host = $_SERVER['SERVER_NAME'];
		$host = (substr($host, 0, 3) == 'www') ? substr($host, 4) : $host;		
		$key = "session_".$host;
		
		//register session name space		
		$session = new Zend_Session_Namespace($key);
		
		if($session->config){
			Zend_Registry::set('session',$session);			
			return;
		}
		
		//get data in table config
		$tblConfig = new Models_Config();
			
		$vc_select = $tblConfig->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
		//echo $vc_select;		
		$row = $tblConfig->fetchAll($vc_select);
			
		//put data to session
		for($i=0;$i<$row->count();$i++){
			$name = $row->getRow($i)->name;
			$value = $row->getRow($i)->value;
			$config[$name]=$value;
			//$session->$name = $value;			
		}
		if(!isset($config["page_size"])) $config["page_size"] =10;
		$session->config = $config;
		Zend_Registry::set('session',$session);		
	}		
	
	/*
	 * Created by truonghm
	 * Created date 30/10/2010
	 * init cache  
	 */
	protected function _initCache()
	{
		$frontendOptions = array(
		   'lifetime' => 7200, // cache lifetime of 2 hours
		   'automatic_serialization' => true
		);
		
		$backendOptions = array(
		    'cache_dir' => OFX_ROOT_DIR.'/public/cache/' // Directory where to put the cache files
		);
		
		$cache = Zend_Cache::factory('Core',
		                             'File',
		                             $frontendOptions,
		                             $backendOptions);

		Zend_Registry::set("cache", $cache);
	}
	
	/**
	 * created by truonghm
	 * created date 01/09/2010
	 * khoi tao thu vien js, header app
	 */
    protected function _initView()
    {
       $view = new Zend_View(); 
       $view->headMeta()->appendHttpEquiv('Content-Type','text/html; charset=utf-8');     
       $view->headLink()->headLink(array('rel' => 'favicon','href' => '/favicon.ico'),'PREPEND');
	   //$view->headScript()->appendFile('/js/lib.js','text/javascript');       
       return $view;
    }
    
    /**
	 * created by truonghm
	 * created date 01/09/2010
	 * loader plugin
	 */
    protected function _initPlugin()
    {
    	$front = Zend_Controller_Front::getInstance();
    	$front ->throwExceptions(true)
    		   ->registerPlugin(new Ofx_Plugin_Translate())
    		   ->registerPlugin(new Ofx_Plugin_Themes());
    }
    
    /**
	 * created by truonghm
	 * created date 01/09/2010
	 * loader plugin
	 */
    protected function _initFront()
    {    	
    	$front = Zend_Controller_Front::getInstance();
		$front->addModuleDirectory(OFX_APP_DIR."/modules")
		 	  ->setDefaultModule("dashboard")
		      ->setDefaultControllerName("index")
		      ->setDefaultAction("index");		
    }	
}